#include"stc.h"
int i=0;
int j=0;
int matran[32][32];

void matrankhoitao()
{  i=0;
	while(i < 32)
	{
		 j = 0;
		while(j < 32)
		{
			if(i==0&& j==0) matran[i][j] =1;
			else matran[i][j] =matran[i][j-1]+1;
			j = j +1;
		}
		i = i+1;
	}
}
void xepmatran()
{ 
	 i=0;
	while(i<32)
	{
		 j=0;
	while(j<32)
	{		
			if((i <8 && j < 8)||(i < 8 && j > 23) ||(i>23 && j < 8) ||(i>23 && j>23)
				||(i>7 && i<24 && j>7 && j<24))
			{
				matran[i][j] = matran[i][j];
			}
			else
			{
				matran[i][j] = matran[31-i][31-j];
			}
			j = j +1;
		}
		i = i +1;
	}	
}
void hienthi()
{
	 i=0;
	while(i<32)
	{
		 j=0;
		while(j<32)
		{   			
			if(matran[i][j]<10)
				PutStr("   ");
			if(matran[i][j]>=10 && matran[i][j]<100)
				PutStr("  ");
			if(matran[i][j]>=100)
				PutStr(" ");
			PutInt(matran[i][j]);
			j=j+1;
		}
		PutStr("\n");
		i=i+1;
	}

}
int check(int sum)
{
	 i = 0;
	int sumhang = 0;
	int sumCot  = 0;
	int sumDuongCheo1 = 0;
	int sumDuongCheo2 = 0;	
	while(i < 32)
	{
		 j = 0;
		sumhang = 0;
		sumCot = 0;
		while(j < 32)
		{
			sumhang =sumhang +  matran[i][j];
			sumCot = sumCot + matran[j][i];
			if(i == j)sumDuongCheo1 = sumDuongCheo1 +matran[i][i];
			if( j == 31 -j)sumDuongCheo1 = sumDuongCheo2 +matran[31-i][i];
			
			j = j + 1;
		}
		i = i + 1;
	}

	if(sumhang == sumCot == sumDuongCheo1 == sumDuongCheo2) 
	return sumhang;
}

void main()
{
	matrankhoitao();
	xepmatran();
	int sum=16400;
	if(check(sum))
	{
		PutStr("Magic Square with 32 x32 size  : \n");
		hienthi();
		PutStr("\nsum ");
		PutInt(sum);
		PutStr("\n");
	}
}
